<template>
  <div id="app">
    <div class="container">
      <div class="close">
        <span class="iconfont iconicon-test" @click="$router.back()"></span>
      </div>
      <div class="logo">
        <span class="iconfont iconnew"></span>
      </div>
      <div class="inputs">
        <!-- <input placeholder="请输入手机号" class="input" /> -->

        <!-- <hm_input :value="user.username" @getvalue="getvalue"></hm_input> -->

        <hm_input
          type="text"
          v-model="user.username"
          placeholder="请输入用户名"
          :rules="/^(\d{5,6})$|^(1\d{10})$/"
          msg="用户名或者手机号输入不正确"
        ></hm_input>

        <hm_input
          type="password"
          v-model="user.password"
          placeholder="请输入密码"
          :rules="/^\S{3,16}$/"
          msg="请输入3-16位的密码"
        ></hm_input>
        <!-- <input placeholder="密码" class="input" type="password" /> -->
      </div>
      <p class="tips">
        没有账号？
        <a href="#/register" class>去注册</a>
      </p>
      <!-- <div class="button">登录按钮</div>
      -->
      <hm_button @click="login" type="danger">登录</hm_button>
    </div>
  </div>
</template>

<script>
import hm_button from "@/components/hm_button.vue";
import hm_input from "@/components/hm_input.vue";
import { userLogin } from "@/apis/user.js";

export default {
  components: {
    hm_button,
    hm_input
  },
  methods: {
    login(e) {
      // console.log(this.user);
      if (
        /^1[35789]\d{9}|admin$/.test(this.user.username) &&
        /^\S{3,16}$/.test(this.user.password)
      ) {
        userLogin(this.user)
          .then(res => {
            console.log(res);
            localStorage.setItem("hmtt_token", res.data.data.token);
            localStorage.setItem("hmtt_id", res.data.data.user.id);
            let redirect = location.href.split("=")[1];
            if (redirect) {
              location.href = decodeURIComponent(redirect);
            } else {
              this.$router.push({ path: `/personal/${res.data.data.user.id}` });
            }
          })
          .catch(err => {
            console.log(err);
          });
      } else {
        this.$toast({
          message: "手机号或密码输入不合法",
          position: "top"
        });
      }
    }
  },
  data() {
    return {
      user: {
        username: "18318602737",
        password: "123456"
      }
    };
  }
};
</script>

<style lang="less" scoped>
.container {
  padding: 20px;
}
.close {
  span {
    font-size: 27 / 360 * 100vw;
  }
}
.logo {
  display: flex;
  justify-content: center;

  span {
    display: block;
    font-size: 126 / 360 * 100vw;
    color: #d81e06;
  }
}
.inputs {
  input {
    margin-bottom: 20px;
  }
}
.tips {
  text-align: right;
  margin-bottom: 20px;
  a {
    color: #3385ff;
  }
}
</style>